import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import pulpcore.Stage;
import pulpcore.CoreSystem;
import pulpcore.sound.Sound;
import pulpcore.image.CoreFont;
import pulpcore.image.CoreImage;
import pulpcore.scene.Scene2D;
import pulpcore.sprite.Button;
import pulpcore.sprite.Group;
import pulpcore.sprite.Sprite;
import pulpcore.util.StringUtil;
import pulpfizz.physics.BodyUtils;
import pulpfizz.physics.Material;
import pulpfizz.pulp.body.Java2DBodySprite;

/**
 * Created by will
 * Date: May 25, 2008 11:44:06 PM
 */
public class AboutScene extends Scene2D {
    private pulpcore.sprite.Label pelter;
    Arena physics;
    Group buttons = new Group();
    private Button ok;
    private Button jordanbros;
    private Button muteButton;

    public void load() {
        setupWorld();

        muteButton = new Button(CoreImage.load("mute.png").split(3,2), Stage.getWidth()-10, Stage.getHeight()-10, true);
        muteButton.setSelected(!CoreSystem.isMute());
        muteButton.setPixelLevelChecks(false);
        muteButton.setAnchor(Sprite.SOUTH_EAST);
        add(muteButton);


        CoreFont font = CoreFont.load("digital.font.png").scale(1.5);
        pelter = new pulpcore.sprite.Label(font,"PELTER", Stage.getWidth()/2,Stage.getHeight()/4);
        pelter.setAnchor(Sprite.CENTER);
        add(pelter);

        final pulpcore.sprite.Label by = new pulpcore.sprite.Label(CoreFont.getSystemFont(),"by Will Jordan",Stage.getWidth()/2,Stage.getHeight()/4 + 30);
        by.setAnchor(Sprite.CENTER);
        buttons.add(by);

        CoreFont textFont = CoreFont.getSystemFont();
                String aboutText = "Pelter is easy to play, but the CPU is tough to beat!\n" +
                        "Just click the mouse button to fire a pellet.\n" +
                        "Push the targets to the far edge of the arena to score points.\n" +
                        "Your shot will charge up a bit when not firing.\nThat's it, have fun!" +
                        "\n\n" +
                        "Pelter was developed in Java using the JBox2D physics engine and the PulpCore applet framework.";
                String[] textLines = StringUtil.wordWrap(aboutText, textFont, Stage.getWidth()/2);
                Group aboutLabel = new Group();
                int x=Stage.getWidth()/4,y=250;
                for (String line : textLines) {
                    // Add the sprite
                    pulpcore.sprite.Label label = new pulpcore.sprite.Label(textFont, line, x, y);
                    aboutLabel.add(label);
                    y += label.height.get() + 5;
                }
                add(aboutLabel);

        ok = Button.createLabeledButton("OK", Stage.getWidth()/2,Stage.getHeight() - 80);
        ok.setAnchor(Sprite.CENTER);
        buttons.add(ok);
        add(buttons);

        CoreImage sjb = CoreImage.load("jordanbros.png");
        CoreImage[] images = {sjb,sjb,sjb};
        jordanbros = new Button(images, Stage.getWidth() - 42,Stage.getHeight()-42);
        jordanbros.setAnchor(Sprite.SOUTH_EAST);
        add(jordanbros);

    }

    private void setupWorld() {
        physics = new Arena(20);
        physics.setAnchor(Sprite.CENTER);
        add(physics);
        physics.fitToStage();
        physics.scores.visible.set(false);
        physics.setGravity(new Vec2(0,0));

        // World container.
        Body body = BodyUtils.createHollowBox(physics.getWorld(), 0, 0, 40, 30, 2f);
        Material.DEAD_WEIGHT().apply(body);
        physics.add(new Java2DBodySprite(body, physics, 1, java.awt.Color.DARK_GRAY, java.awt.Color.black, java.awt.Color.black));
    }

    Sound plop = Sound.load("plop2.ogg");
    public void update(int elapsedTime) {
        if(ok.isClicked()) {
            Stage.popScene();
            plop.play();
        }
        if(jordanbros.isClicked()) {
            CoreSystem.showDocument("http://www.gamefight.org");
            plop.play();
        }
        if (muteButton.isClicked()) {
            CoreSystem.setMute(!muteButton.isSelected());
            plop.play();
        }
    }
}
